import 'dart:ffi';

import 'package:bilibili/db/hi_cache.dart';
import 'package:bilibili/http/core/hi_net.dart';
import 'package:bilibili/http/request/base_request.dart';
import 'package:bilibili/http/request/login_request.dart';
import 'package:bilibili/http/request/registration_request.dart';

class LoginDao {
  static const BOARDING_PASS = 'boarding-pass';

  static login(String username, String password) {
    return _send(username, password);
  }

  static registration(String username, String password) {}

  static _send(String username, String password, {imoocId, orderId}) async {
    BaseRequest request;
    if (imoocId != null && orderId != null) {
      request = LoginRequest();
    } else {
      request = RegistrationRequest();
    }
    request.add("username", username).add("password", password);

    var result = await HiNet.getInstance().fire(request);
    if (result != null) {
      HiCache.getInstance().setString(BOARDING_PASS, result["id"].toString());
    }

    return result;
  }

  static getBoardingPass() {
    return HiCache.getInstance().get(BOARDING_PASS);
  }
}
